package vampirism.core.client;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import vampirism.core.shared.Bullet;
import vampirism.core.shared.Creator;
import vampirism.core.shared.Util;

public class ClientInput {
	
	public MainGame main;
	
	public boolean[] prevMovement = new boolean[4];
	
	private int mouseX, mouseY;
	
	public ClientInput(MainGame main){
		this.main = main;
	}
	
	public void update(){
		
		checkMove();
		checkShoot();
	}
	
	private void checkMove(){
		
		//Has the input changed from the previous frame
		boolean inputChanged = false;
		
		for(int i = 0; i < 4; i++){
			prevMovement[i] = main.myPlayer.moving[i];//Remember the previous state - to know if we need to send new Move packet
			main.myPlayer.moving[i] = false;//If no arrow keys are pressed - player is not moving
		}
		
		//Moving my player locally depending on the input keys
		if(Gdx.input.isKeyPressed(Keys.D)){main.myPlayer.moving[0] = true;}
		if(Gdx.input.isKeyPressed(Keys.A)){main.myPlayer.moving[1] = true;}
		if(Gdx.input.isKeyPressed(Keys.S)){main.myPlayer.moving[2] = true;}
		if(Gdx.input.isKeyPressed(Keys.W)){main.myPlayer.moving[3] = true;}
		
		//Checking if input has changed
		for(int i = 0; i < 4; i++){
			if(prevMovement[i] != main.myPlayer.moving[i]){
				inputChanged = true; break;
			}
		}
		
		//If have pressed/released new keys - send packet to server
		if(inputChanged)main.clientSide.simulateMove(main.myPlayer.moving);
	}
	
	private void checkShoot(){
		short shootDelay = (short)((40-main.myPlayer.statsTotal[2])*10);
		
		if(main.myPlayer.lastShootTime + shootDelay < System.currentTimeMillis()){
			if(Gdx.input.isButtonPressed(0)){
				
				mouseX = Gdx.input.getX();
				mouseY = Gdx.input.getY();
				
				float angle = Util.getAngle(main.W/2,main.H/2,mouseX,mouseY);
				
				//Send shooting packet to server
				main.clientSide.simulateShoot(angle);
				
				//Instantiate bullet locally
				Bullet newBullet = Creator.createBullet(main.myPlayer, angle);
				main.bullets.add(newBullet);
				
				main.myPlayer.lastShootTime = System.currentTimeMillis();
			}
		}
	}
	

}
